package LeetCode.leetcode114;

class Solution {
    TreeNode newRoot, p;
    public void flatten(TreeNode root) {
        if (root == null) return;
        newRoot = new TreeNode(root.val);
        p = newRoot;
        dfs(root);
        root.left = null;
        root.right = newRoot.right.right;
    }
    public void dfs(TreeNode root) {
        if (root == null) return;
        p.left = null;
        p.right = new TreeNode(root.val);
        p = p.right;
        dfs(root.left);
        dfs(root.right);
    }
}